package com.wenguo.bussines.dao.base;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.List;
import java.util.Map;

import org.springframework.dao.DataAccessException;

import com.wenguo.Constants;
import com.wenguo.bussines.spring.MySqlSessionDaoSupport;

@SuppressWarnings("unchecked")
public abstract class BaseMybatis3Dao<E, PK extends Serializable> extends
        MySqlSessionDaoSupport implements EntityDao<E, PK> {

    protected Class<E> clazz;
    protected String clazzName;

    public BaseMybatis3Dao() {
        // 通过范型反射，取得在子类中定义的class.
        clazz = (Class<E>) ((ParameterizedType) getClass()
                .getGenericSuperclass()).getActualTypeArguments()[0];
        clazzName = clazz.getSimpleName();
        // 即取model名称，区分大小写
        // System.out.println(clazzName+","+clazz.getName());
    }

    public E get(PK pk) throws DataAccessException {
        return (E) super.getSqlSession().selectOne(
                clazzName + Constants.mybatis.GET_BY_ID, pk);
    }

    public List<E> getByEntity(E e) throws DataAccessException {
        return super.getSqlSession().selectList(
                clazzName + Constants.mybatis.GET_BY_ENTITY, e);
    }

    @Override
    public List<E> getByMap(Map<Object, Object> m) throws DataAccessException {
        return super.getSqlSession().selectList(
                clazzName + Constants.mybatis.GET_BY_MAP, m);
    }

    public List<E> getByDefined(Map<Object, Object> m) throws DataAccessException {
        return super.getSqlSession().selectList(clazzName + Constants.mybatis.GET_BY_DEFINED, m);
    }
    
    public Object getCount(E e) throws DataAccessException {
        return super.getSqlSession().selectOne(clazzName + Constants.mybatis.GET_COUNT, e);
    }

    public List<E> getAll() throws DataAccessException {
        return super.getSqlSession().selectList(clazzName + Constants.mybatis.GET_ALL);
    }

    public boolean save(E e) throws DataAccessException {
        try {
			super.getSqlSession().insert(clazzName + Constants.mybatis.INSERT, e);
			return true;
		} catch (Exception e1) {
			e1.printStackTrace();
			return false;
		}
    }

    public boolean saveOrUpdate(E e) throws DataAccessException {
    	return false;
    }

    public boolean update(E e) throws DataAccessException {
        try {
			super.getSqlSession().update(clazzName + Constants.mybatis.UPDATE, e);
			return true;
		} catch (Exception e1) {
			e1.printStackTrace();
			return false;
		}
    }
    
    public boolean updatePwd(E e) throws DataAccessException {
        try {
			super.getSqlSession().update(clazzName + Constants.mybatis.UPDATE_PWD, e);
			return true;
		} catch (Exception e1) {
			e1.printStackTrace();
			return false;
		}
    }

    public boolean delete(PK pk) throws DataAccessException {
        try {
			super.getSqlSession().delete(clazzName + Constants.mybatis.DELETE, pk);
			return true;
		} catch (Exception e1) {
			e1.printStackTrace();
			return false;
		}
    }

}
